﻿using System;
using System.Windows;
using System.Linq;
using Microsoft.Windows.Controls.Ribbon;
using OODCRM.Common;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using OODCRM.ViewModel;

namespace OODCRM
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : RibbonWindow
    {

        public MainWindow()
        {
            InitializeComponent();
            Application.Current.Properties["MainWindowRef"] = new WeakReference(this);
            this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            Dialogs.UserLogin dialog = new Dialogs.UserLogin();

            // Không hiển thị khi ko đăng nhập đc
            if (dialog.ShowDialog() == true)
            {
                dataContext = new CRMDataDataContext(Properties.Settings.Default.User_ConnectionString);
                tabControl.Visibility = Visibility.Visible;
                RibbonModel.TabDanhMuc.IsEnabled = true;
                RibbonModel.TabDoiTuong.IsEnabled = true;
                RibbonModel.TabHome.IsEnabled = true;
                stackLeftSide.IsEnabled = true;
                tabControl.SelectedItem = null;
            }
            else
            {
                tabControl.Visibility = Visibility.Collapsed;
                RibbonModel.TabDanhMuc.IsEnabled = false;
                RibbonModel.TabDoiTuong.IsEnabled = false;
                RibbonModel.TabHome.IsEnabled = false;
                stackLeftSide.IsEnabled = false;
                
            }

            
        }


        #region Set State

        /// <summary>
        /// Set the state for the feature change
        /// </summary>
        /// <param name="isEnabled">state</param>
        public void SetState(bool? isEnabled, Feature feature)
        {
            if (isEnabled == null) return;
            if (isEnabled.Value)
            {
                currentFeature = feature;
                EnableFeature(currentFeature);
            }
            else
            {
                DisableFeature(currentFeature);
            }
        }

        /// <summary>
        /// Relayout the UI to enable feature when navigation
        /// </summary>
        /// <param name="feature"></param>
        private void EnableFeature(Feature feature)
        {
            switch (feature)
            {

                case Feature.SanPham:
                    tabSanPham.IsSelected = true;
                    if (listSanPham == null) listSanPham = new ObservableCollection<SANPHAM>(
                        dataContext.SANPHAMs
                        );
                    if (dgrSanPham.ItemsSource != listSanPham)
                        dgrSanPham.ItemsSource = listSanPham;

                    break;
                case Feature.ChienDich:
                    tabChienDich.IsSelected = true;
                    if (listChienDich == null) listChienDich = new ObservableCollection<CHIENDICH>(
                        dataContext.CHIENDICHes
                        );
                    if (dgrChienDich.ItemsSource != listChienDich)
                        dgrChienDich.ItemsSource = listChienDich;

                    break;

                case Feature.ToChuc:
                    tabToChuc.IsSelected = true;
                    if (listToChuc == null) listToChuc = new ObservableCollection<TOCHUC>(
                        dataContext.TOCHUCs
                        );
                    if (dgrToChuc.ItemsSource != listToChuc)
                        dgrToChuc.ItemsSource = listToChuc;
                    dgrToChuc.SelectedItem = null;
                    break;

                case Feature.LienHe:
                    tabLienHe.IsSelected = true;
                    if (listLienHe == null) listLienHe = new ObservableCollection<LIENHE>(
                        dataContext.LIENHEs
                        );
                    if (dgrLienHe.ItemsSource != listLienHe)
                        dgrLienHe.ItemsSource = listLienHe;
                    break;

                case Feature.CoHoi:
                    tabCoHoi.IsSelected = true;
                    if (listCoHoi == null) listCoHoi = new ObservableCollection<COHOI>(
                        dataContext.COHOIs
                        );
                    if (dgrCoHoi.ItemsSource != listCoHoi)
                        dgrCoHoi.ItemsSource = listCoHoi;
                    break;                
                case Feature.TinhHuong:
                    tabTinhHuong.IsSelected = true;
                    if (listTinhHuong == null) listTinhHuong = new ObservableCollection<TINHHUONG>(
                        dataContext.TINHHUONGs
                        );
                    if (dgrTinhHuong.ItemsSource != listTinhHuong)
                        dgrTinhHuong.ItemsSource = listTinhHuong;
                    break;
                case Feature.TiemNang:
                    tabTiemNang.IsSelected = true;
                    if (listTiemNang == null) listTiemNang = new ObservableCollection<TIEMNANG>(
                        dataContext.TIEMNANGs
                        );
                    if (dgrTiemNang.ItemsSource != listTiemNang)
                        dgrTiemNang.ItemsSource = listTiemNang;
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// Relayout the UI and Ribbon controls to enable feature
        /// </summary>
        /// <param name="feature"></param>
        private void DisableFeature(Feature feature)
        {
            switch (feature)
            {
                case Feature.SanPham:
                    tabSanPham.IsSelected = false;
                    break;
                case Feature.ChienDich:
                    tabChienDich.IsSelected = false;
                    break;
                case Feature.ToChuc:
                    tabToChuc.IsSelected = false;
                    break;
                case Feature.LienHe:
                    tabLienHe.IsSelected = false;
                    break;
                case Feature.CoHoi:
                    tabCoHoi.IsSelected = false;
                    break;                
                case Feature.TinhHuong:
                    tabTinhHuong.IsSelected = false;
                    break;
                case Feature.TiemNang:
                    tabTiemNang.IsSelected = false;
                    break;
                default:
                    break;
            }
        }

        #endregion Set State


        #region Dialogging

        private void EnableDialog(CRMDialog dialog)
        {
            switch (dialog)
            {
                case CRMDialog.ThemToChuc:
                    TOCHUC item = dicDialogData[CRMDialog.ThemToChuc] as TOCHUC;
                    if (item == null) return;

                    // Set status of the control for Key field
                    switch (dicDialogMode[CRMDialog.ThemToChuc])
                    {
                        case DialogMode.Sua:
                            tbxMaHieu_tabThemToChucDialog.IsEnabled = false;
                            break;
                        default:
                            tbxMaHieu_tabThemToChucDialog.IsEnabled = true;
                            break;
                    }

                    // Set binding for dialog
                    tabControl_tabThemToChucDialog.DataContext = null;
                    tabControl_tabThemToChucDialog.DataContext = item;

                    // Cho du lieu vao cac combo box
                    cbxChuQuan_tabThemToChucDialog.ItemsSource = dataContext.TOCHUCs;
                    cbxLoaiToChuc_tabThemToChucDialog.ItemsSource = dataContext.LOAITOCHUCs;
                    cbxLinhVuc_tabThemToChucDialog.ItemsSource = dataContext.LINHVUCs;
                    cbxSLNhanVien_tabThemToChucDialog.ItemsSource = dataContext.SLNHANVIENs;
                    cbxDoanhThuNam_tabThemToChucDialog.ItemsSource = dataContext.DOANHTHUNAMs;
                    //cbxPhanLoai_tabThemToChucDialog.ItemsSource = dataContext.PHANLOAITOCHUCs;
                    cbxLoaiSoHuu_tabThemToChucDialog.ItemsSource = dataContext.LOAISOHUUs;
                    cbxChuSoHuu_tabThemToChucDialog.ItemsSource = dataContext.Users;
                    cbxQuocGiaTT_tabThemToChucDialog.ItemsSource = dataContext.VITRIDIALies.Where(x => x.CapVTDL == "QG");
                    cbxQuocGiaCH_tabThemToChucDialog.ItemsSource = dataContext.VITRIDIALies.Where(x => x.CapVTDL == "QG");

                    // Chinh lai selected item cho cac combo box ko the binding tu dong
                    cbxChuQuan_tabThemToChucDialog.SelectedItem = dataContext.TOCHUCs.FirstOrDefault(x => x.MaHieu == item.MaChuQuan);
                    cbxChuSoHuu_tabThemToChucDialog.SelectedItem = dataContext.Users.FirstOrDefault(x => x.Id == item.DOITUONG.User.Id);
                    //var ctphanloai = item.CHITIETPHANLOAITOCHUCs.FirstOrDefault();
                    //if (ctphanloai != null)
                    //    cbxPhanLoai_tabThemToChucDialog.SelectedItem = ctphanloai.PHANLOAITOCHUC;

                    // Enable the dialog
                    tabThemToChucDialog.IsSelected = true;

                    break;

                case CRMDialog.ThemChienDich:
                    CHIENDICH itemChienDich = dicDialogData[CRMDialog.ThemChienDich] as CHIENDICH;
                    if (itemChienDich == null) return;
                    tabThemChienDichDialog.DataContext = null;
                    tabThemChienDichDialog.DataContext = itemChienDich;
                    cbxLoai_tabThemChienDichDialog.ItemsSource = dataContext.LOAICHIENDICHes;
                    cbxTinhTrang_tabThemChienDichDialog.ItemsSource = dataContext.TINHTRANGCHIENDICHes;
                    cbxChuSoHuu_tabThemChienDichDialog.ItemsSource = dataContext.Users;
                     cbxChuSoHuu_tabThemChienDichDialog.SelectedItem=  dataContext.Users.FirstOrDefault(x => x.Id == itemChienDich.DOITUONG.User.Id);
                    tabThemChienDichDialog.IsSelected = true;  
                    break;

                case CRMDialog.ThemCoHoi:
                    COHOI itemCoHoi = dicDialogData[CRMDialog.ThemCoHoi] as COHOI;
                    if (itemCoHoi == null) return;
                    tabThemCoHoiDialog.DataContext = null;
                    tabThemCoHoiDialog.DataContext = itemCoHoi;       
                    cbxToChuc_tabThemCoHoiDialog.ItemsSource = dataContext.TOCHUCs;
                    cbxChienDich_tabThemCoHoiDialog.ItemsSource = dataContext.CHIENDICHes;
                    cbxNguonGoc_tabThemCoHoiDialog.ItemsSource = dataContext.NGUONGOCs;
                    cbxGiaiDoan_tabThemCoHoiDialog.ItemsSource = dataContext.GIAIDOANCOHOIs;
                    cbxBuocTiepTheo_tabThemCoHoiDialog.ItemsSource = dataContext.BUOCTIEPTHEOCOHOIs;
                    cbxLyDoThangThua_tabThemCoHoiDialog.ItemsSource = dataContext.LYDOTHANGTHUACOHOIs;
                    cbxChuSoHuu_tabThemCoHoiDialog.ItemsSource = dataContext.Users;
                    cbxChuSoHuu_tabThemCoHoiDialog.SelectedItem=dataContext.Users.FirstOrDefault(x => x.Id == itemCoHoi.DOITUONG.User.Id);
                    tabThemCoHoiDialog.IsSelected = true;
                    break;

                case CRMDialog.ThemLienHe:
                    LIENHE itemLienHe = dicDialogData[CRMDialog.ThemLienHe] as LIENHE;
                    if (itemLienHe == null) return;
                    tabThemLienHeDialog.DataContext = null;
                    tabThemLienHeDialog.DataContext = itemLienHe;
                    cbxXungHo_tabThemLienHeDialog.ItemsSource = dataContext.XUNGHOs;
                    cbxToChuc_tabThemLienHeDialog.ItemsSource = dataContext.TOCHUCs;
                    cbxNguonGoc_tabThemLienHeDialog.ItemsSource = dataContext.NGUONGOCs;
                    cbxChuSoHuu_tabThemLienHeDialog.ItemsSource = dataContext.Users;
                    cbxQuocGiaCQ_tabThemLienHeDialog.ItemsSource = dataContext.VITRIDIALies.Where(x => x.CapVTDL == "QG");
                    cbxQuocGiaNR_tabThemLienHeDialog.ItemsSource = dataContext.VITRIDIALies.Where(x => x.CapVTDL == "QG");
                    cbxChuSoHuu_tabThemLienHeDialog.SelectedItem =dataContext.Users.FirstOrDefault(x => x.Id == itemLienHe.DOITUONG.User.Id);
                    tabThemLienHeDialog.IsSelected = true;
                    break;

                case CRMDialog.ThemSanPham:
                    SANPHAM itemSanPham = dicDialogData[CRMDialog.ThemSanPham] as SANPHAM;
                    if (itemSanPham == null) return;
                    cbxPhanLoai.ItemsSource = dataContext.PHANLOAISANPHAMs;
                    tabThemSanPhamDialog.DataContext = itemSanPham;
                    tabThemSanPhamDialog.IsSelected = true;
                    break;
                case CRMDialog.ThemTinhHuong:
                    TINHHUONG itemTinhHuong = dicDialogData[CRMDialog.ThemTinhHuong] as TINHHUONG;
                    if (itemTinhHuong == null) return;
                    tabThemTinhHuongDialog.DataContext = null;
                    tabThemTinhHuongDialog.DataContext = itemTinhHuong;
                    cbxTinhTrang_tabThemTinhHuongDialog.ItemsSource = dataContext.TINHTRANGTINHHUONGs;
                    cbxPhanLoai_tabThemTinhHuongDialog.ItemsSource = dataContext.PHANLOAITINHHUONGs;
                    cbxNguonGoc_tabThemTinhHuongDialog.ItemsSource = dataContext.NGUONGOCTINHHUONGs;
                    cbxLoai_tabThemTinhHuongDialog.ItemsSource = dataContext.LOAITINHHUONGs;
                    cbxNguyenNhan_tabThemTinhHuongDialog.ItemsSource = dataContext.NGUYENNHANTINHHUONGs;
                    cbxChuSoHuu_tabThemTinhHuongDialog.ItemsSource = dataContext.Users;
                   cbxChuSoHuu_tabThemTinhHuongDialog.SelectedItem = dataContext.Users.FirstOrDefault(x => x.Id == itemTinhHuong.DOITUONG.User.Id);
                    tabThemTinhHuongDialog.IsSelected = true;
                    break;

                case CRMDialog.ThemTiemNang:
                    TIEMNANG itemTiemNang = dicDialogData[CRMDialog.ThemTiemNang] as TIEMNANG;
                    if (itemTiemNang == null) return;
                    tabThemTiemNangDialog.DataContext = null;
                    tabThemTiemNangDialog.DataContext = itemTiemNang;
                    cbxXungHo_tabThemTiemNangDialog.ItemsSource = dataContext.XUNGHOs;
                    cbxSLNhanVien_tabThemTiemNangDialog.ItemsSource = dataContext.SLNHANVIENs;
                    cbxDoanhThu_tabThemTiemNangDialog.ItemsSource = dataContext.DOANHTHUNAMs;
                    cbxNguonGoc_tabThemTiemNangDialog.ItemsSource = dataContext.NGUONGOCs;                    
                    cbxQuocGiaTT_tabThemToChucDialog.ItemsSource = dataContext.VITRIDIALies.Where(x => x.CapVTDL == "QG");
                    cbxQuocGiaCH_tabThemToChucDialog.ItemsSource = dataContext.VITRIDIALies.Where(x => x.CapVTDL == "QG");
                    cbxChuSoHuu_tabThemTiemNangDialog.ItemsSource = dataContext.Users;
                    cbxChuSoHuu_tabThemTiemNangDialog.SelectedItem = dataContext.Users.FirstOrDefault(x => x.Id == itemTiemNang.DOITUONG.User.Id);
                    tabThemTiemNangDialog.IsSelected = true;
                    break;
                default:
                    break;
            }
        }

        #endregion Dialogging


        #region Data

        private CRMDataDataContext dataContext = new CRMDataDataContext(Properties.Settings.Default.User_ConnectionString);
        internal Feature currentFeature;
        private Dictionary<CRMDialog, DialogMode> dicDialogMode = new Dictionary<CRMDialog, DialogMode>();
        private Dictionary<CRMDialog, object> dicDialogData = new Dictionary<CRMDialog, object>();

        ObservableCollection<TOCHUC> listToChuc;
        ObservableCollection<CHIENDICH> listChienDich;
        ObservableCollection<COHOI> listCoHoi;
        ObservableCollection<LIENHE> listLienHe;
        ObservableCollection<TIEMNANG> listTiemNang;
        ObservableCollection<TINHHUONG> listTinhHuong;
        ObservableCollection<SANPHAM> listSanPham;

        #endregion Data

    }
}
